package com.hq.yusuan.actions;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.eredlab.g4.ccl.datastructure.Dto;
import org.eredlab.g4.ccl.json.JsonHelper;
import org.eredlab.g4.ccl.util.G4Constants;
import org.eredlab.g4.ccl.util.G4Utils;
import org.eredlab.g4.rif.web.BaseAction;
import org.eredlab.g4.rif.web.CommonActionForm;

import com.hq.szgl.service.ShouZhiService;
import com.hq.yusuan.service.YuSuanService;

public class YuSuanAction extends BaseAction {
	private YuSuanService yusuanservice=(YuSuanService)this.getService("yusuanService");
	//预算管理初始化页面！初始化同时查询数据！
	public ActionForward init(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception 
	{
		CommonActionForm fForm = (CommonActionForm) form;
		Dto dto = fForm.getParamAsDto(request);
		System.out.println("提示：进入了预算管理初始化逻辑");
		return mapping.findForward("init");
	}
	//查询项目信息的方法
	public ActionForward queryProjects(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		CommonActionForm fForm = (CommonActionForm) form;
		Dto dto = fForm.getParamAsDto(request);
		System.out.println("提示：正在查询项目信息"+dto);
		List list = g4Reader.queryForPage("yusuanDemo.queryProjects", dto);
		String jsonString = JsonHelper.encodeList2PageJson(list,(Integer)this.g4Reader.queryForObject("yusuanDemo.queryProjectsCount", dto), null);
		this.write(jsonString, response);
		return null;
	}
	//查询对应项目的预算信息
	public ActionForward queryYuSuanByPrjsn(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		CommonActionForm fForm = (CommonActionForm) form;
		Dto dto = fForm.getParamAsDto(request);
		System.out.println("提示：正在查询当前项目的预算信息");
		List list=this.g4Reader.queryForPage("yusuanDemo.queryYuSuanByPrjsn", dto);
		String jsonString=JsonHelper.encodeList2PageJson(list,(Integer)this.g4Reader.queryForObject("yusuanDemo.queryYuSuanByPrjsnCount", dto),G4Constants.FORMAT_Date);
		this.write(jsonString, response);
		return null;
	}
	//添加预算信息
	public synchronized ActionForward addYuSuan(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		CommonActionForm fForm = (CommonActionForm) form;
		Dto dto = fForm.getParamAsDto(request);
		System.out.println("提示：正在给给项目编号为"+dto.getAsString("prj_sn")+"添加预算信息。。。。"+dto);
		this.yusuanservice.addYuSuan(dto);
		System.out.println("执行完了");
		this.setOkTipMsg("添加成功！", response);
		return null;
	}
	//删除预算信息
	public ActionForward deleteYuSuan(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		CommonActionForm fForm = (CommonActionForm) form;
		Dto dto = fForm.getParamAsDto(request);
		System.out.println("提示：正在删除项目编号为"+dto.getAsString("prj_sn")+"的预算信息。。。。"+dto);
		this.yusuanservice.deleteYuSuan(dto);
		this.setOkTipMsg("预算信息删除成功！", response);
		return null;
	}
	//添加修改信息
	public synchronized ActionForward updateYuSuan(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		CommonActionForm fForm = (CommonActionForm) form;
		Dto dto = fForm.getParamAsDto(request);
		System.out.println("提示：正在修改项目编号为"+dto.getAsString("prj_sn")+"的预算信息。。。。"+dto);
		this.yusuanservice.updateYuSuan(dto);
		this.setOkTipMsg("预算信息修改成功！", response);
		return null;
	}
	//获得可以添加的费用类型
	public  ActionForward queryMoneyType(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		CommonActionForm fForm = (CommonActionForm) form;
		Dto dto = fForm.getParamAsDto(request);
		System.out.println("提示：正在加载项目编号为"+dto.getAsString("prj_sn")+"的可用费用类型。。。。"+dto);
		List list=this.g4Reader.queryForList("yusuanDemo.queryMoneyType", dto);
		this.write(JsonHelper.encodeList2PageJson(list, list.size(),null), response);
		return null;
	}
}
